package com.yanqu.road.server.logic.activity.condition.chatactivity;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


/**
 * @author ：zfr
 * @date ：2021/9/27 11:11
 */
public class ChatActivityCondition extends BaseActivityCondition {

    public ChatActivityCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        String chatContent = (String) object;
        if (chatContent.equals(activityConditionInfo.getExtendParam()) && !isGetReward()) {
            Property reward = activityConditionInfo.getReward();
            baseActivityData.getUserActivityConditionData(activityConditionInfo.getConditionId()).setGetReward(true);
            sendMailReward(reward);
            baseActivityData.finish(activityConditionInfo.getConditionId());
        }
    }

    private void sendMailReward(Property reward) {
        String language = player.getLanguage();
        String mailTitle = MultipleLanguageMgr.getContent(activityInfo.getParam5(), language);
        String mailContent = MultipleLanguageMgr.getContent(activityInfo.getParam6(), language);
        MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.ChatActivity.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.ChatActivity.getValue(), handle);
    }

    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
    }
}
